import {
    GetOrderList,
    StockUpBatch,
    GetSelfFetchOrder,
    SetFinishOrder,
    GetWxCode
} from '@/services/order/orderPickUpList';
import {catchException} from '@/utils/utils';
export  default {
    namespace: 'orderPickUp',

    state: {
        orderList: [],
        selfFetchOrder:{},
        wxCode:{}
    },

    effects: {
        // 获取订单列表
        *GetOrderList({payload, callBack}, {call, put}) {
            const data = yield call(GetOrderList, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetOrderList',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })            
        },

        //完成备货
        *StockUpBatch({payload, callBack}, {call, put}) {
            const data = yield call(StockUpBatch, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                 }
            })             
          },

          //
          *GetSelfFetchOrder({payload, callBack}, {call, put}) {
            const data = yield call(GetSelfFetchOrder, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetSelfFetchOrder',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })             
        },

        //核销
        *SetFinishOrder({payload, callBack}, {call, put}) {
            const data = yield call(SetFinishOrder, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                 }
            })             
          },

          //小程序码
          *GetWxCode({payload, callBack}, {call, put}) {
            const data = yield call(GetWxCode, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetWxCode',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })             
        },
    },

    reducers: {
        saveGetOrderList(state, {payload}) {
            return {
                ...state,
                orderList: payload.dataList,
            };
        },
        saveGetSelfFetchOrder(state, {payload}) {
            return {
                ...state,
                selfFetchOrder: payload.dataList,
            };
        },
        saveGetWxCode(state, {payload}) {
            return {
                ...state,
                wxCode: payload.dataList,
            };
        },
    }
}